package com.example.online_order.conrtoller;


import com.example.online_order.pojo.ClientAccount;

import com.example.online_order.pojo.Result;
import com.example.online_order.service.ClientAccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/client")
@CrossOrigin
@Slf4j
public class ClientAccountController {

//    新增（注册），查询（ 全表查询 用户名查询 ，模糊查询），删除（注销），修改,登录

    @Autowired
    ClientAccountService clientAccountService;



    @PostMapping("/regist")
    public Result addclient(@RequestBody ClientAccount clientAccount){
        Integer res = clientAccountService.addClient(clientAccount);
        if (res != null ){
            return new Result(clientAccount);
        }else{
            return  Result.fail("Client account already",500);
        }
    }

    @PutMapping("/reset")
    public Result reSetPassword(@RequestBody ClientAccount clientAccount){
        Integer res = clientAccountService.updateClientPassword(clientAccount);
        if (res == 1){
            return new Result("update password success");
        } else{
            return  Result.fail("update password failed",555);
        }
    }



    @DeleteMapping("/del/{id}")
    public Result delClient(@PathVariable("id") Integer id){
        Integer res = clientAccountService.deleteClient(id);
        if (res == 1){
            return new Result("delete success");
        }else{
            return  Result.fail("delete failed",444);
        }
    }

    @PutMapping("/update")
    public Result updateClient(@RequestBody ClientAccount clientAccount){
        Integer res = clientAccountService.updateClient(clientAccount);
        if (res == 1){
            return new Result("update success");
        } else{
            return  Result.fail("update failed",444);
        }
    }

    @GetMapping("/getAll")
    public Result getAllClient(){
        return new Result(clientAccountService.getAllClient());
    }

    @GetMapping("/getLike/{userName}")
    public Result getByLikeUsername(@PathVariable("userName") String userName){
        return  new Result(clientAccountService.findClientLikeUsername(userName));
    }


    @GetMapping ("/ByName/{userName}")
    public Result getByUsername(@PathVariable ("userName") String userName){
        return  new Result(clientAccountService.findClientByUsername(userName));
    }
}
